"""

@name:python学习
@author:yuki
@projectname:起步

"""
# 批量扫描指定网段内的存活IP
# 网段：192.168.0.0/24 (192.168.0.1~192.168.0.255)
# 考虑如何才能更快速地扫描出结果
# 前置模块：IPy, ping命令的使用
# import subprocess
# import threading
# import IPy
# def ping(address):
#     pgreturn = subprocess.Popen("ping -w 1 {0} \n".format(ip),stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
#     if pgreturn:
#         print(f"{address}: down")
#     else:
#         print(f"{address}: up")
#
#
# def ipright(ipadress):
#     try:
#         global ip
#         ip = IPy.IP(ipadress)
#         return True
#     except:
#         print("The ipadress is not right")
#         return False
#
# while True:
#     ips = input("please input your ip segment：")
#     if ipright(ips) == True:
#         break
#     elif iprigt(ips) == False:
#         continue
#
# def main():
#     for i in ip:
#         t = threading.Thread(target=ping, args=(i,))
#         t.start()
# if __name__ == '__main__':
#     main()

#
# 装饰器装饰类
# 定义一个装饰器sorted_by_create_time实现装饰类的装饰器
# 装饰器的功能：按创建时间将实例进行排序
#
# global i
# i = 0
# class sorted_by_create_time(object):
#     def __init__(self, cl):
#         # 初始化操作在此完成
#         self.__cl = cl
#
#     # 实现__call__方法，表示对象是一个可调用对象，可以像调用函数一样进行调用。
#     def __call__(self, *args, **kwargs):
#         print(f"this is the '{i}'")
#         self.__cl()
#
#
# @sorted_by_create_time
# def start():
#     global i
#     print("go")
#     i += 1
#
# start()
# start(2)

# 完善三创购物系统
# 所有用户存放在Excel中（用户名，密码，余额）
# 定义装饰器login_required，给需要的功能添加装饰器
# 查看超市物品，不需要登录
# 购物及查看余额，需要登录
# 先验证用户是否已经登录（如果用户未登录提示“用户未登录，请登录后重试”，并跳
# 转到功能菜单）


# def exit():
#     if not cart:
#         print("your shop cart do npt have anything")
#     else:
#         print(f"you have buy this goods: {cart}")
#     print(f"your budget have:{budget}")
#     sys.exit(0)






####未完成

import sys
itemlst = {'F00001':{'name':'Apple','price':'1.2'},'F00002':{'name':'Banana','price':'5.5'}}
userlst = {'root':'123456'}
cart = set()
budget = 0


def login_required(func):
    def _login_required(*args):
        if user == None:
            print('You have not login')
        else:
            func(*args)
    return  _login_required

def operate():
    print('★'.center(20, "="))
    print('用户操作'.center(20,"="))
    print('1.登'.center(20,"="))
    print('2.注册'.center(20,"="))
    print('3.购物'.center(20,"="))
    print('4.充值'.center(20,"="))
    print('5.查看余额'.center(20,"="))
    print('6.查看购物清单'.center(20,"="))
    print('7.查看商品列表'.center(20,"="))
    print('★'.center(20,"="))

def exit():
    if not cart:
        print("your shop cart do npt have anything")
    else:
        print(f"you have buy this goods: {cart}")
    print(f"your budget have:{budget}")
    sys.exit(0)

def login(str):
    if user in userlst:
        password = input("input your password:")
        if password == userlst.get('root'):
            print("login successful!\nplease inpuy your budget later")
            Budget()
        elif password == "exit":
            exit()
        else:
            print("your password is not right,please input again")
            login(user)
    else:
        print("your user is not exist，please inpuy again")

def Budget():
    global budget
    A = input("please input your budget")
    if A == "exit":
        exit()
    elif A.isdigit():
        budget = float(A)
        shopping()
    else:
        print("The character types which you input is not right,please input again")
        Budget()

def shopping():
    global budget
    X = input("if you want see shop iteml please input buy\nexit input exit\nsee shop cart input cart\n")
    if X == "buy":
        for key in itemlst:
            print(f"product id is “{key}”,information is {itemlst[key]}")
        print("please input the id which you want to buy:")
        buying = input()
        if buying in itemlst and budget > float(itemlst.get(buying).get("price")):
            cart.add(itemlst.get(buying).get("name"))
            budget -= float(itemlst.get(buying).get("price"))
            print(f"You have buy {itemlst.get(buying).get('name')}")
            shopping()
        elif buying in itemlst and float(budget) < float(itemlst.get(buying).get("price")):
            print("Your budget is not enough")
            shopping()
        elif buying == "exit" :
             exit()
        else:
            print("The id is nou right,please input again")
            shopping()
    elif X == "cart":
        if not cart:
            print("your shop cart do npt have anything")
            shopping()
        else:
            print(f"Your shopping cart have following items:{cart}")
            shopping()
    elif X == "exit":
        exit()
    else:
        print("The input is nou right,please input again")
        shopping()

while True:
        user=input("please input your username：")
        if user == "exit":
            exit()
        login(user)